<?php
namespace app\admin\model;
use app\common\enum\Setting as SettingEnum;
use think\Cache;
use think\Model;

/**
 *  基础设置
 * Class BaseSetting
 * @package app\admin\model
 */
class BaseSetting extends Model
{

    protected $name='base_setting';
    protected $createTime = false;

    /**
     * 获取器: 转义数组格式
     * @param $value
     * @return mixed
     */
    public function getValuesAttr($value){
        return json_decode($value, true);
    }

    /**
     * 修改器: 转义成json格式
     * @param $value
     * @return string
     */
    public function setValuesAttr($value){
        return json_encode($value);
    }
    /**
     * 获取设置项信息
     * @param $key
     * @return null|static
     * @throws \think\exception\DbException
     */
    public static function detail($key){
        return self::get(compact('key'));
    }
    /**
     * 更新系统设置
     * @param $key
     * @param $values
     * @return bool
     * @throws \think\exception\DbException
     */
    public function edit($key, $values){
        $model = self::detail($key) ?: $this;
        // 删除系统设置缓存
        Cache::rm('base_setting');
        return $model->save([
                'key' => $key,
                'describe' => SettingEnum::data()[$key]['describe'],
                'values' => $values,

            ]) !== false;
    }

    /**
     * 获取指定项设置
     * @param $key
     * @return array
     */
    public static function getItem($key){
        $data = self::getAll($key);
        return isset($data[$key]) ? $data[$key]['values'] : [];
    }
    /**
     * 全局缓存: 系统设置
     * @return array|mixed
     */
    public static function getAll($key){
        $static = new static;
        if (!$data = Cache::get('base_setting')) {
            //$setting = $static::all(compact($key));
            $setting = $static::all();
            $data = empty($setting) ? [] : array_column(collection($setting)->toArray(), null, 'key');
            Cache::tag('cache')->set('base_setting', $data);
        }
        return $static->getMergeData($data);
    }
    /**
     * 合并用户设置与默认数据
     * @param $userData
     * @return array
     */
    private function getMergeData($userData){
        $defaultData = $this->defaultData();
        return array_merge_multiple($defaultData, $userData);
    }
    public static function getItemValue($key){
        $data=self::where('key','agent')->find();
        return isset($data['values'][$key]) ? $data['values'][$key] : [];
    }
    /**
     * 默认配置
     * @param null|string $storeName
     * @return array
     */
    public function defaultData($storeName = null){
        return [
            // 上传设置
            'storage' => [
                'key' => 'storage',
                'describe' => '上传设置',
                'values' => [
                    'default' => 'local',
                    'engine' => [
                        'local' => [],
                        'qiniu' => [
                            'bucket' => '',
                            'access_key' => '',
                            'secret_key' => '',
                            'domain' => 'http://'
                        ],
                        'aliyun' => [
                            'bucket' => '',
                            'access_key_id' => '',
                            'access_key_secret' => '',
                            'domain' => 'http://'
                        ],
                        'qcloud' => [
                            'bucket' => '',
                            'region' => '',
                            'secret_id' => '',
                            'secret_key' => '',
                            'domain' => 'http://'
                        ],
                    ]
                ],
            ],

        ];
    }
}